package com.example.spring_musicserver_1113.controller;

/**
 * @version 1.0
 * @Author 作者名
 * @Date 2024/11/13 14:48
 * @注释
 */

import com.example.spring_musicserver_1113.mapper.UserMapper;
import com.example.spring_musicserver_1113.model.User;
import com.example.spring_musicserver_1113.tools.Constant;
import com.example.spring_musicserver_1113.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @RequestMapping("/login1")
    public ResponseBodyMessage<User> login1(@RequestParam String username,
                                     @RequestParam String password,
                                           HttpServletRequest request) {
        User loginUser = new User();
        loginUser.setUsername(username);
        loginUser.setPassword(password);
        User user = userMapper.login(loginUser);
        if (user == null) {
            System.out.println("登录失败！");
            return new ResponseBodyMessage<>(-1,"登录失败",loginUser);
        } else {
            //request.getSession().setAttribute("USERINFO_SESSION_KEY",user);
            //上述代码由于USERINFO_SESSION_KEY是字符串由于容易写错，所以
            //创建枚举类constant，来定义一个常量USERINFO_SESSION_KEY为一个字符串，这样就方便我们后期调用
            //关于httpservletrequest是客户端向服务器发送数据报，HttpServletRequest对象代表客户端的请求，
            // 当客户端通过HTTP协议访问服务器时，HTTP请求头中的所有信息都封装在这个对象中，
            // 通过这个对象提供的方法，可以获得客户端请求的所有信息。
            //所谓的该对象的getsession，即用于获取与当前请求关联的会话对象，
            // 如果不存在会话，则创建一个新的会话。
            request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,user);
            return new ResponseBodyMessage<>(0,"登录成功",loginUser);
        }
    }
//-------------------------------------------------------------

    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(@RequestParam String username,
                                           @RequestParam String password,
                                           HttpServletRequest request) {
        User user = userMapper.selectByName(username);
        if (user == null) {
            System.out.println("登录失败！");
            return new ResponseBodyMessage<>(-1,"用户名或密码错误",user);
        } else {
            boolean flag = bCryptPasswordEncoder.matches(password,user.getPassword());
            if(!flag){
                return new ResponseBodyMessage<>(-1,"用户名或密码错误！",user);
            }
            request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,user);
            return new ResponseBodyMessage<>(0,"登录成功",user);
        }
    }
}
